package com.mall.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * Redis限流配置
 */
@Configuration
public class RedisRateLimiterConfig {
    
    /**
     * 默认限流配置（每秒50个请求，突发100个请求）
     */
    @Bean
    @Primary
    public RedisRateLimiter defaultRedisRateLimiter() {
        return new RedisRateLimiter(50, 100);
    }
    
    /**
     * 认证服务限流器（每秒20个请求，突发50个请求）
     */
    @Bean("authRateLimiter")
    public RedisRateLimiter authRedisRateLimiter() {
        return new RedisRateLimiter(20, 50);
    }
    
    /**
     * 用户服务限流器（每秒30个请求，突发60个请求）
     */
    @Bean("userRateLimiter")
    public RedisRateLimiter userRedisRateLimiter() {
        return new RedisRateLimiter(30, 60);
    }
    
    /**
     * 产品服务限流器（每秒40个请求，突发80个请求）
     */
    @Bean("productRateLimiter")
    public RedisRateLimiter productRedisRateLimiter() {
        return new RedisRateLimiter(40, 80);
    }
    
    /**
     * 订单服务限流器（每秒20个请求，突发40个请求）
     */
    @Bean("orderRateLimiter")
    public RedisRateLimiter orderRedisRateLimiter() {
        return new RedisRateLimiter(20, 40);
    }
} 